#include "CallbackObject.h"
#include "vr.h"
#include <iostream>
using std::cout;
using std::endl;
CallbackObject::CallbackObject()
{
}

/*
This is only a demo for parsing the coordiante attributes.
The same applies to Color, Texture Coordiantes and so on

*/
void CallbackObject::PreTraverseNode(VRNode * node)
{
    //handle the children node of an IndexFaceSet object
    //these children node contain the coordinates of an indexFaceSet
    switch(node->type){
	case VR_COORDINATE:
	    {
		//parse all coordinates in a coordiante list of a single object
		VRCoordinate* coords = (VRCoordinate*)(node);
		for(int i=0;i<coords->point.numValues;i++){
		    cout<<coords->point.values[i][0]<<" "<<
			  coords->point.values[i][1]<<" "<<
			  coords->point.values[i][2]<<endl;
		}
		//parse the coordIndex from the parent
		VRIndexedFaceSet* vrIndexFaceSet  = (VRIndexedFaceSet*)(node->parent);
		//parse the index array, each primitive is terminated by -1
		
		for(int i=0;i<vrIndexFaceSet->coordIndex.numValues;i++)
		{
		    //parse the indeces of a single primitive;
		    for(int j=i;j<vrIndexFaceSet->coordIndex.numValues;j++)
			{
				if(vrIndexFaceSet->coordIndex.values[j] == -1)
				{
					i=j;
					break;
				}
		    }
		}
	    }
	    break;
	    case VR_COLOR://for color index
	    {
		//parse all coordinates in a coordiante list of a single object
		VRColor* colors = (VRColor*)(node);
		for(int i=0;i<colors->color.numValues;i++){
		    cout<<"R "<<colors->color.values[i][0]<<" G "<<
			  colors->color.values[i][1]<<" B "<<
			  colors->color.values[i][2]<<endl;
		}
		//parse the colorIndex from the parent
		VRIndexedFaceSet* vrIndexFaceSet  = (VRIndexedFaceSet*)(node->parent);
		//parse the index array, each primitive is terminated by -1
		
		for(int i=0;i<vrIndexFaceSet->colorIndex.numValues;i++){
		    //parse the indeces of a single primitive;
		    for(int j=i;j<vrIndexFaceSet->colorIndex.numValues;j++){
			if(vrIndexFaceSet->colorIndex.values[j] == -1){
			    i=j;
			    break;
			}
			cout<<vrIndexFaceSet->colorIndex.values[j];
		    }
		}
		    
	    }
	    break;
	case VR_TEXTURE_COORDINATE:
	    {
		//parse all coordinates in a coordiante list of a single object
		VRTextureCoordinate* texCoords = (VRTextureCoordinate*)(node);
		for(int i=0;i<texCoords->point.numValues;i++){
		    cout<<"U "<<texCoords->point.values[i][0]<<" V "<<
			  texCoords->point.values[i][1]<<endl;
		}
		//parse the colorIndex from the parent
		VRIndexedFaceSet* vrIndexFaceSet  = (VRIndexedFaceSet*)(node->parent);
		//parse the index array, each primitive is terminated by -1
		
		for(int i=0;i<vrIndexFaceSet->texCoordIndex.numValues;i++){
		    //parse the indeces of a single primitive;
		    for(int j=i;j<vrIndexFaceSet->texCoordIndex.numValues;j++){
			if(vrIndexFaceSet->texCoordIndex.values[j] == -1){
			    i=j;
			    break;
			}
			cout<<vrIndexFaceSet->texCoordIndex.values[j];
		    }
		}
	    }
	    break;
	case VR_INDEXED_FACE_SET:
	    //this is good place to allocate your polygonal mesh object
	    //when traversing its children the object will be ready for storing the
	    //data
	    
	    break;
	case VR_IMAGE_TEXTURE:
	    {
		//for each node that contains images as parametric textures
		//
		VRImageTexture * textureImage = (VRImageTexture*)(node);
		std::string fileName = textureImage->url.values[0];
		cout<<"Texture file name = "<<fileName.c_str()<<endl;
	    }
	    break;
	default:
	    cout<<"Node type = "<<node->type<<" unhandeled here<<"<<endl;

    }
	
}
void CallbackObject::PostTraverseNode(VRNode * node)
{
}
